/*
 * RegexUtils.java
 * business
 *
 * Created by ChenTao on 2016/9/9.
 *
 * Copyright (c) 2016年 yidiandao. All rights reserved.
 */

package com.yidiandao.business.utils;

import com.yidiandao.business.constant.Preferences;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegexUtils {
    /**
     * @param phoneNo 加星号过的电话号码
     */
    public static String dealPhoneNo(String phoneNo) {
        return phoneNo.substring(0, 3) + "****" + phoneNo.substring(7);
    }

    /**
     * 通过正则表达式判断是否为手机号
     */
    public static boolean isPhoneNumber(String phoneString) {
        String regex =
                PreferenceUtils.getString(UIUtils.getContext(), Preferences.MOBILE_PATTERN, true);
        if (PreferenceUtils.STRING_DEFAULT.equals(regex)) {
            // 默认正则
            regex = "^1[34578]\\d{9}$";
        }

        // String c = String.valueOf(phoneString.charAt(0));
        // String string = "5689";
        // if (string.contains(c)) {
        //     format = "^[5689][0-9]{7}$";
        // } else {
        //     format = "^(13[0-9]|15[012356789]|17[03678]|18[0-9]|14[57])[0-9]{8}$";
        // }

        return isMatch(regex, phoneString);
    }

    /**
     * 固定电话正则判断
     */
    public static boolean isTelephone(String text) {
        String regex = "^0[0-9]{2,3}\\d[0-9]{7,8}$";
        return isMatch(regex, text);
    }

    /**
     * 字符串正则校验
     *
     * @param regex  正则表达式
     * @param string 需要检验的字符串
     */
    public static boolean isMatch(String regex, String string) {
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(string);
        return matcher.matches();
    }

    /**
     * 通过正则表达式判断密码
     */
    public static boolean isPsd(String psd) {
        String format = "^[A-Za-z0-9/@/!/#/$/%/^/&/*/./~/,/_/。/]{6,32}$";
        return isMatch(format, psd);
    }

    /**
     * 验证身份证号是否符合规则
     *
     * @param text 身份证号
     */
    public static boolean personIdValidation(String text) {
        String regx = "[0-9]{17}x";
        String reg1 = "[0-9]{15}";
        String regex = "[0-9]{18}";
        return text.matches(regx) || text.matches(reg1) || text.matches(regex);
    }

    /**
     * 验证昵称是否符合规则
     *
     * @param text 昵称
     */
    public static boolean checkNickName(String text) {
        String format = "^[\u4e00-\u9fa5A-Za-z0-9/./!/~/_/！]{1,15}$";
        return isMatch(format, text);
    }

    /**
     * 验证数字
     */
    public static boolean checkContent(String connect) {
        String format = "^[0-9]+$";
        return isMatch(format, connect);
    }

    /**
     * 校验是否是float
     */
    public static boolean isFloat(String content) {
        String format = "^(-?\\d+)(\\.\\d+)?$";
        return isMatch(format, content);
    }

    /**
     * 验证邮箱
     */
    public static boolean checkEmail(String connect) {
        String format = "^[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}$";
        return isMatch(format, connect);
    }

    /**
     * 验证qq
     */
    public static boolean checkQQ(String connect) {
        String format = "^[1-9][0-9]{4,14}$";
        return isMatch(format, connect);
    }

    /**
     * 判断是不是纯数字
     */
    public static boolean isNumeric(String str) {
        String format = "[0-9]*";
        return isMatch(format, str);
    }
}
